<template>
  <div class>
    商品列表
    <div class="goods-list">
      <router-link tag="div"  :to="'/home/goodsinfo/'+item.id" class="goods-item" v-for="item in goodlist" :key="item.id">
        <img :src="item.img_url" alt />
        <h1 class="title">{{item.title}}</h1>
        <div class="info">
          <p class="price">
            <span class="now">￥{{item.sell_price}}</span>
            <span class="old">￥{{item.market_price}}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{item.stock_quantity}}件</span>
          </p>
        </div>
      </router-link>
      <mt-button type="danger" size="large" style="margin-top:10px" @click="getMore()">加载更多</mt-button>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  name: "",
  data() {
    return {
      // 页码
      page: 1,
      //   商品列表
      goodlist: [],
      //   节流阀默认为false
      isloaded: false,
    };
  },
  components: {},
  created() {
    this.getGoodsListByPage();
  },
  mounted() {},
  methods: {
    //   根据页码获取商品列表
    async getGoodsListByPage() {
      const { data } = await this.$http.get(
        "/api/getgoods?pageindex=" + this.page
      );
      if (data.status === 0) {
        if (data.message.length > 0) {
          this.goodlist = this.goodlist.concat(data.message);
        } else {
          // 数据加载完毕，没有数据了
          this.isloaded = true;
          Toast("没有更多数据了!");
        }
      }
    },
    getMore() {
        // 数据加载完毕了，直接return
      if (this.isloaded) return;
      this.page++;
      this.getGoodsListByPage();
    },
  },
};
</script>
<style lang='scss' scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;

  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    // 这里设置flex的纵向对齐方式是为了让img靠上对齐，info靠下对齐，title自己定位
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    img {
      width: 100%;
      //   防止刚加载的一瞬间没有高度
      min-height: 170px;
    }
    .title {
      font-size: 14px;
    }

    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
}
</style>
